//
//  LiveSpectatorListView.swift
//  GLUZSwift
//
//  Created by yu on 2025/3/24.
//  Copyright © 2025 考拉🐨. All rights reserved.
//

import Foundation
import UIKit

import Kingfisher
import Reusable


/// 直播观众列表
class LiveSpectatorListView: UIView {
    let spectatorNumLabel: UILabel = UILabel()
    let tableView: UITableView = UITableView()
    
    var dataSoure: [LiveSpectatorModel] = [] {
        didSet {
            spectatorNumLabel.text =  "观众".localized(.live) + "　·　" + "\(dataSoure.count)"
            
            tableView.reloadData()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        layer.cornerRadius = 20
        layer.maskedCorners = CACornerMask(rawValue: CACornerMask.layerMinXMinYCorner.rawValue | CACornerMask.layerMaxXMinYCorner.rawValue )
        layer.masksToBounds = true

        spectatorNumLabel.textColor = UIColor(hex: "#1C1D1D")
        spectatorNumLabel.font = .systemFont(ofSize: 16)
        spectatorNumLabel.text = "观众".localized(.live) + "　·　０"
        
        tableView.dataSource = self
        tableView.delegate = self
        tableView.rowHeight = 60
        tableView.register(cellType: LiveSpectatorListCell.self)
        
//        let stackV = UIStackView(arrangedSubviews: [spectatorNumLabel, tableView])
//        addSubview(stackV)
        
        addSubview(spectatorNumLabel)
        addSubview(tableView)
        
        spectatorNumLabel.snp.makeConstraints { make in
//            make.top.equalToSuperview().offset(40)
            make.centerY.equalTo(self.top).offset(30)
            make.left.equalToSuperview().offset(20)
        }
        
        tableView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(60)
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
            make.bottom.equalToSuperview().offset(20)
            
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension LiveSpectatorListView: UITableViewDelegate {
    
}

extension LiveSpectatorListView: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSoure.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(for: indexPath, cellType: LiveSpectatorListCell.self)
        cell.model = dataSoure[indexPath.row]
        return cell
    }
    
}

extension LiveSpectatorListView {
    
}


/// 直播观众列表
class LiveSpectatorListCell: UITableViewCell {
    
    var userIconImageView: UIImageView!
    var userNameLabel: UILabel!
    var markLabel: UILabel!
    
    var model: LiveSpectatorModel? {
        didSet {
            if let model = model {
                userIconImageView.kf.setImage(with: URL(string: model.userIconURL))
                userNameLabel.text = model.userName
                markLabel.text = model.mark
            }
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        userIconImageView = UIImageView()
        userIconImageView.snp.makeConstraints { make in
            make.width.height.equalTo(60)
        }
        
        userNameLabel = UILabel()
        userNameLabel.font = .systemFont(ofSize: 16)
        userNameLabel.textColor = UIColor(hex: "#1C1D1D")
        // 抗拉伸
        userNameLabel.setContentHuggingPriority(.defaultHigh, for: .horizontal)
        
        markLabel = UILabel()
        markLabel.font = .systemFont(ofSize: 16)
        markLabel.textColor =  .gray //UIColor(hex: "#1C1D1D")
        // 抗压缩
        markLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        
        let stackV = UIStackView(arrangedSubviews: [userIconImageView, userNameLabel, markLabel])
        stackV.axis = .horizontal
        stackV.distribution = .fill
        stackV.alignment = .center
        stackV.spacing = 10
        contentView.addSubview(stackV)
        
        stackV.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20))
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension LiveSpectatorListCell: NibReusable {}

class LiveSpectatorModel: BaseModel {
    
    required init() {
        fatalError("init() has not been implemented")
    }
    
    var userIconURL: String = ""
    var userName: String = ""
    var mark: String = ""
    
}

